package com.springboot.redis;

import com.springboot.redis.model.User;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author liuwei
 * @version 2017/7/18 上午12:58
 * @modified TODO
 * @description 功能描述
 * @see
 * @since 1.0
 */
@Configuration
public class RedisConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public JedisPoolConfig getRedisConfig(){
        JedisPoolConfig config = new JedisPoolConfig();
        return config;
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.redis")
    public JedisConnectionFactory jedisConnectionFactory(){
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setPoolConfig(this.getRedisConfig());
        return factory;
    }

    @Bean
    public RedisTemplate<Long , User> redisTemplate(JedisConnectionFactory factory){
        RedisTemplate<Long , User> redisTemplate = new RedisTemplate<Long , User>();
        redisTemplate.setConnectionFactory(factory);

        return redisTemplate;
    }
}
